// pages/activity/vote/index/index.js

let utilApp = require('../../../../utils/app');
const {
  regeneratorRuntime
} = global;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    isRank: true,
    count: 0,
    index: 0,
    popImgArr: ['daminggong', 'dayanta', 'kunming', 'shibo'],
    plantAddArr: ["大明宫", "大雁塔", "昆明湖", "世博园"],
    plantAddress: "大明宫",
    plantAdd: "",
    address: "../../../../image/dayanta-tan.png",
    time: {
      hours: '00',
      minutes: '00',
      seconds: '00'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      popImgArr: ['daminggong', 'dayanta', 'kunming', 'shibo'],
      plantAddArr: ["大明宫", "大雁塔", "昆明湖", "世博园"]
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function() {
    let that = this;
    let wxGetSetting = utilApp.promisify(wx.getSetting);
    let isUserInfo = await wxGetSetting()
    if (isUserInfo.authSetting['scope.userInfo']) {
      that.getRemainTime().then(async function(res) {
        if (res.remainTime >0) {
          // 活动未结束
          setInterval(function() {
            let time = utilApp.leftTimer(res.dueTime * 1000)
            that.setData({
              time
            }) 
          }, 1000)
        } else {
          // 活动已结束
          let userInfo = wx.getStorageSync('userInfo');
          let isChoose = await utilApp.doPost('Area/isChoose', {
            openId: userInfo.openId
          });
          if (isChoose.data.status == 1) {
            let areaRanking = await utilApp.doPost('Area/areaRanking', {
              openId: userInfo.openId
            });
            if (areaRanking.data.my_paiming == 1) {
              utilApp.showModal('恭喜您', `您选择的地点:${that.data.plantAddArr[isChoose.data.area - 1]}获得第${areaRanking.data.my_paiming}名，请前往公众号回复'19'领取奖励`, '复制券码', function () {
                wx.setClipboardData({
                  data: '19',
                  success: function(res) {
                    wx.getClipboardData({
                      success: function(res) {
                        wx.showToast({
                          title: '复制成功'
                        })
                      }
                    })
                  }
                })
              }, function () { }, false)
            } else {
              utilApp.showModal('提示', `您选择的地点:${that.data.plantAddArr[isChoose.data.area - 1]}获得第${areaRanking.data.my_paiming}名，请前往公众号回复'20'领取参与奖`, '复制券码', function() {
                wx.setClipboardData({
                  data: '20',
                  success: function (res) {
                    wx.getClipboardData({
                      success: function (res) {
                        wx.showToast({
                          title: '复制成功'
                        })
                      }
                    })
                  }
                })
              }, function() {}, false)
            }
          } else {
            utilApp.showModal('提示', '该活动已结束，前往活动中心参与其他活动吧', '活动中心', function() {
              wx.redirectTo({
                url: '../../../activityList/activityList'
              })
            }, function() {}, false)
          }
        }
        let userInfo = wx.getStorageSync('userInfo');
        let isChoose = await utilApp.doPost('Area/isChoose', {
          openId: userInfo.openId
        });
        if (isChoose.data.status) {
          that.setData({
            isRank: false
          })
        }
      })
    } else {
      utilApp.isGetUserInfo('../../../authorization/authorization', '../activity/vote/index/index')
    }
  },
  // 剩余时间
  async getRemainTime() {
    let res = await utilApp.doPost('Area/getDueTime');
    let newTime = (Date.parse(new Date())) / 1000;
    return {
      remainTime: res.data.due_time - newTime,
      dueTime: res.data.due_time
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let that = this;
    that.setData({
      sharePop: false
    })
    if (ops.from === 'button') {
      // 来自页面内转发按钮 
      console.log(ops.target)
    }
    return {
      title: '西安移动投票赢话费',
      path: '/pages/activity/vote/index/index',
      imageUrl: '../../../../image/index-bottom.jpg',
      success: function(res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
        })
      },
      fail: function(res) {
        utilApp.showErr('分享失败', 2000)
      }
    }
  },

  plan: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.current;
    let userInfo = wx.getStorageSync('userInfo');
    that.getRemainTime().then(async function(res) {
      if (res.remainTime > 0) { 
        // 活动未结束
        let chooseArea = await utilApp.doPost('Area/chooseArea', {
          openId: userInfo.openId,
          area: index
        }); 
        if (chooseArea.data.status == 1) {
          that.setData({
            isShow: false,
            count: 0,
            plantAddress: that.data.plantAddArr[index - 1],
            address: "../../../../image/" + that.data.popImgArr[index - 1] + "-tan.png",
          })
        } else {
          utilApp.showModal('提示', `您已选择过地点：${that.data.plantAddArr[chooseArea.data.area-1]},无需重复选择`, '地点排行', function() {
            wx.navigateTo({
              url: '../rank/rank',
            })
          }, function() {}, true)
        }
      } else {
        // 活动已结束
        utilApp.showModal('提示', '该活动已结束，前往活动中心参与其他活动吧', '活动中心', function() {
          wx.redirectTo({
            url: '../../../activityList/activityList'
          })
        }, function() {}, false)
      }
    })





    // 

  },
  close: function() {
    var that = this;
    that.setData({
      isShow: true,
    });
  },
  confirm: function() {
    wx.navigateTo({
      url: '/pages/activity/vote/rank/rank',
    })
  }
})